package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.common.enums.ResponseCodeEnum;
import com.example.common.utils.ApiResult;
import com.example.model.entity.TUser;
import com.example.model.query.TEquipmentQuery;
import com.example.model.query.TUserQuery;
import com.example.model.query.TWarningQuery;
import com.example.service.TUserService;
import com.example.service.TWarningService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 告警账户配置表
 * @author zhangr
 * @Description Created on 2020-12-22
 */

@RestController
@Controller
@RequestMapping("tUser")
@Api(value = "告警账户配置管理",tags={"告警账户配置管理接口"})
public class TUserController {

    @Autowired
    private TUserService userService;

    @ApiOperation(value = "修改账户详情", notes = "账户详情", httpMethod = "GET")
    @PostMapping("/saveUser")
    public ApiResult<Boolean> saveUser(@RequestBody TUser user){
        try {
            QueryWrapper<TUser> queryWrapper = new QueryWrapper<TUser>();
            queryWrapper.lambda().eq(TUser::getUserType,"idispatch")
                    .eq(TUser::getIsValid,1L);
            List<TUser> userList = userService.list(queryWrapper);
            if(userList.size() == 0){
                user.setCreateDate(LocalDateTime.now());
                user.setUserType("idispatch");
                userService.save(user);
            }else {
                TUser myUser = userList.get(0);
                myUser.setUserName(user.getUserName());
                myUser.setPassword(user.getPassword());
                myUser.setUpdateDate(LocalDateTime.now());
                userService.updateById(myUser);
            }
            return new ApiResult<>(Boolean.TRUE);
        }   catch (Exception e) {
            e.printStackTrace();
            return new ApiResult<>(ResponseCodeEnum.MALL_SERVICE_PAGE_AD_FALSE);
        }
    }

    /*
     * 详情
     *
     */
    @ApiOperation(value = "账户详情", notes = "账户详情", httpMethod = "GET")
    @GetMapping("/getUserInfo")
    public ApiResult<TUser> getInfo(){
        try{
            TUser user = new TUser();
            QueryWrapper<TUser> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(TUser::getUserType,"idispatch")
                    .eq(TUser::getIsValid,1L);
            List<TUser> userList = userService.list(queryWrapper);
            if(userList.size() > 0){
                user = userList.get(0);
            }
            return new ApiResult<>(user);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult<>(ResponseCodeEnum.MALL_SERVICE_SAVE_AD_FALSE);
        }
    }
}

